package com.mingtu.guards.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polygon;
import com.amap.api.maps.model.PolygonOptions;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.SPTools;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.bean.MyGridInfoBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.mingtu.common.utils.MyUtills.getBounds;
import static com.mingtu.guards.utils.MyConstant.MAX_ZOOM;

public class GridInfoActivity extends MyBaseActivity {

    @BindView(R.id.mapView)
    MapView mapView;
    @BindView(R.id.tv_dept)
    TextView tvDept;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    private AMap aMap;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_grid_info;
    }

    @Override
    protected void initView() {
        setModuleTitle("我的网格化");
    }

    private List<Polygon> polygonList = new ArrayList<>();
    private List<Marker> markerList = new ArrayList<>();

    @Override
    protected void initData() {

    }

    @Override
    protected void getData() {

    }

    @Override
    protected void initMap(Bundle savedInstanceState) {
        super.initMap(savedInstanceState);
        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        //地图模式可选类型：MAP_TYPE_NORMAL,MAP_TYPE_SATELLITE,MAP_TYPE_NIGHT
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
        aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
        aMap.getUiSettings().setZoomControlsEnabled(false);
        aMap.setMaxZoomLevel(MAX_ZOOM);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(Constant.MAP_ZOOM_TWO));
        aMap.setOnMapLoadedListener(new MyOnMapLoadedListener());

        String longitude = SPStaticUtils.getString(SPTools.lng);
        String latitude =SPStaticUtils.getString(SPTools.lat);
        if (!StringUtils.isEmpty(longitude) && !StringUtils.isEmpty(latitude)) {
            LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
            //添加Marker
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(location)
                    .icon(BitmapDescriptorFactory.fromResource(com.mingtu.home.R.mipmap.sb_icon_wz));
            Marker marker = aMap.addMarker(markerOptions);
            markerList.add(marker);
            aMap.moveCamera(CameraUpdateFactory.changeLatLng(location));
        }
    }

    @Override
    protected void onDestroy() {
        if (aMap != null) {
            mapView.onDestroy();
        }

        if (polygonList.size() > 0) {
            for (Polygon polygon : polygonList) {
                polygon.remove();
            }
            polygonList.clear();
        }

        if (markerList.size() > 0) {
            for (Marker marker : markerList) {
                marker.remove();
            }
            markerList.clear();
        }
        super.onDestroy();
    }


    /**
     * 当地图加载完成后回调此方法
     */
    private class MyOnMapLoadedListener implements AMap.OnMapLoadedListener {
        @Override
        public void onMapLoaded() {
            List<MyGridInfoBean.GridInfoBean> list = (List<MyGridInfoBean.GridInfoBean>) getIntent().getSerializableExtra("list");
            if (list != null) {
                if (list.size() > 0) {
                    MyGridInfoBean.GridInfoBean gridInfoBean = list.get(0);
                    String dept = gridInfoBean.getDept();
                    String area = gridInfoBean.getArea();
                    String userName = gridInfoBean.getUserName();
                    String mobile = gridInfoBean.getMobile();


                    if (!StringUtils.isEmpty(dept)) {
                        tvDept.setText(dept + "");
                    }

                    if (!StringUtils.isEmpty(area)) {
                        double myArea = Double.parseDouble(area) / 1000 * 1000;
                        BigDecimal b1 = new BigDecimal(area);
                        BigDecimal b2 = new BigDecimal((1000 * 1000) + "");

                        tvArea.setText(b1.divide(b2,2) + "k㎡");
                    }

                    if (!StringUtils.isEmpty(userName)) {
                        tvName.setText(userName + "");
                    }

                    if (!StringUtils.isEmpty(mobile)) {
                        tvTel.setText(mobile + "");
                    }


                    String points = gridInfoBean.getPoints();
                    if (points.length() > 0) {
                        List<LatLng> latLngs = new ArrayList<>();
                        String MyPoints = points.replaceAll(" ", "").substring(3, points.length() - 3); //先去掉所有的空格，在取中间中间字符数据
                        if (MyPoints.indexOf("，") != -1) {
                            //含有中文符号的时候把中文逗号换成英文逗号
                            MyPoints = MyPoints.replaceAll("，", ",");
                        }
                        String[] objectsArray1 = MyPoints.split("],\\[");
                        for (int i = 0; i < objectsArray1.length; i++) {
                            String[] objectsArray2 = objectsArray1[i].split(",");
                            double lng = Double.parseDouble(objectsArray2[0]);
                            double lat = Double.parseDouble(objectsArray2[1]);
                            LatLng latLng = new LatLng(lat, lng);
                            latLngs.add(latLng);
                        }
                        PolygonOptions polygonOptions = new PolygonOptions();
                        polygonOptions.addAll(latLngs);
                        polygonOptions.strokeWidth(2);

                        polygonOptions.fillColor(context.getResources().getColor(R.color.bg_color_1f1e9));
                        polygonOptions.strokeColor(context.getResources().getColor(R.color.colorPrimary));
                        Polygon mapPolygon = aMap.addPolygon(polygonOptions);
                        polygonList.add(mapPolygon);
                        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getBounds(latLngs), 200));
                    }
                }
            }
        }
    }


}